<template>
     <el-dialog
     :close-on-click-modal="false"
    :title="title" top="2vh" :visible.sync="show" append-to-body class="wp-panel"
    width="80%">
      <div class="wp-panel-header">
        <div>
          <div class="wp-panel-header__desc">{{title}}</div>
          <div>{{ returnOrderInfo.returnNo }}</div>
        </div>
        <div>
          <template v-if="returnOrderInfo.status ===0 || returnOrderInfo.status ===2">
          <el-button @click.native="submitOrder(returnOrderInfo.returnNo)" type="success">提 交</el-button>
          <el-button @click.native="cancelOrder(returnOrderInfo.returnNo)" type="warning">取消订单</el-button>
          </template>
          <template v-else-if="returnOrderInfo.status ===1">
            <el-button @click.native="passOrder(returnOrderInfo.returnNo)" type="success">通 过</el-button>
            <el-button @click.native="rejectOrder(returnOrderInfo.returnNo)" type="warning">驳 回</el-button>
          </template>
          <template v-else-if="returnOrderInfo.status ===3">
            <el-button @click.native="completedOrder(returnOrderInfo.returnNo)" type="success">完 成</el-button>
            <el-button @click.native="cancelOrder(returnOrderInfo.returnNo)" type="warning">取消订单</el-button>
          </template>
           <el-button @click.native="show=false" type="danger">关 闭</el-button>
        </div>
      </div>
      <el-descriptions class="header-descriptions-item" direction="vertical" :column="4">
      <el-descriptions-item label="退货客户">{{ returnOrderInfo.relationName||'--' }}</el-descriptions-item>
      <el-descriptions-item label="退货类型">{{ returnOrderInfo.returnTypeName||'--' }}</el-descriptions-item>
      <el-descriptions-item label="退货金额">{{ returnOrderInfo.returnTotalAmount||'--' }}</el-descriptions-item>
      <el-descriptions-item label="订单状态">{{ returnOrderInfo.statusName||'--' }}</el-descriptions-item>
    </el-descriptions>
        <div class="wp-panel-footer">
            <el-tabs type="border-card" v-model="activeName" @tab-click="handleTabClick">
              <el-tab-pane :key="k1" name="1">
                <span slot="label"><i class="el-icon-date"></i> 退单信息</span>
                <Base-info  :param.sync="returnOrderInfo"  :type="type" @editAfter="refreshList"/>
              </el-tab-pane>
              <el-tab-pane lazy :key="k2" name="2">
                <span slot="label"><i class="el-icon-date"></i> 退货产品</span>
                <Return-product  :param.sync="returnOrderInfo"  :type="type" @editAfter="refreshList"/>
              </el-tab-pane>
            </el-tabs>
        </div>
     </el-dialog>
</template>
<script>
import BaseInfo from './detail/BaseInfo'
import ReturnProduct from './detail/ReturnProduct'
import returnOrder from '@/api/eims/returnOrderAPI'
export default {
  props: {
    title: String,
    type: String,
    param: Object,
    visible: {type: false, default: false}
  },
  components: {BaseInfo, ReturnProduct},
  computed: {
    show: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    param: {
      handler (val) {
        this.returnOrderInfo = val
        this.activeName = '1'
      },
      deep: true,
      immediate: true
    }
  },
  data () {
    return {
      returnOrderInfo: {},
      activeName: '1',
      k1: 1,
      k2: 2
    }
  },
  methods: {
    handleTabClick (tab, event) {
      if (tab.name === '1') {
        this.k1 = Math.random()
      } else if (tab.name === '2') {
        this.k2 = Math.random()
      }
    },
    refreshList (res) {
      this.$emit('refreshList', res)
    },
    submitOrder (orderNo) {
      this.$msgBox.confirm('确认提交退货单', () => {
        returnOrder.updateReturnOrderStatusAPI(orderNo, 1).then(res => {
          if (res.code === 0) {
            this.show = false
            this.refreshList()
          }
        })
      })
    },
    cancelOrder (orderNo) {
      this.$prompt('请输入取消原因', '取消退货单', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: function (v) {
          if (!v || v === undefined || v === '') {
            return '不能为空'
          }
        }
      }).then(({ value }) => {
        const params = {'failureReason': value}
        returnOrder.updateReturnOrderStatusAPI(orderNo, -1, params).then(res => {
          if (res.code === 0) {
            this.show = false
            this.refreshList()
          }
        })
      }).catch(() => {
      })
    },
    passOrder (orderNo) {
      this.$msgBox.confirm('确认退货单通过', () => {
        returnOrder.updateReturnOrderStatusAPI(orderNo, 3).then(res => {
          if (res.code === 0) {
            this.show = false
            this.refreshList()
          } if (res.code === 0) {
            this.show = false
            this.refreshList()
          } if (res.code === 0) {
            this.show = false
            this.refreshList()
          }
        })
      })
    },
    completedOrder (orderNo) {
      this.$msgBox.confirm('确认退货单完成', () => {
        returnOrder.updateReturnOrderStatusAPI(orderNo, 4).then(res => {
          if (res.code === 0) {
            this.show = false
            this.refreshList()
          }
        })
      })
    },
    rejectOrder (orderNo) {
      this.$msgBox.confirm('确认驳回退货单', () => {
        returnOrder.updateReturnOrderStatusAPI(orderNo, 2).then(res => {
          this.show = false
          this.refreshList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
